<template>
  <div class="dashboard-container">

    <div style="margin-left: 20px; margin-top: 15px;">
      最近<el-select
        v-model="day_num"
        placeholder="请选择"
        style="margin-left: 10px; margin-right: 10px; width: 80px;"
        @change="init"
      >
        <el-option
          v-for="item in 30"
          :key="item"
          :label="item"
          :value="item"/>
      </el-select>天
    </div>

    <div id="line_chart" style="width: 1000px;height: 300px; margin-bottom: 20px; margin-left: auto; margin-right: auto;"/>
    <div id="pie_chart" style="width: 500px;height: 320px; margin: auto;"/>
  </div>
</template>

<script>
import echarts from 'echarts'
import order from '@/api/mall/order'
export default {
  name: '',
  data() {
    return {
      day_num: 7,
      line_chart: '',
      pie_chart: '',
      earn7: {},
      categoryRatio: {}
    }
  },
  // 调用
  mounted() {
    this.init(this.day_num)
  },
  methods: {
    init(day_num) {
      order.getEarnInSeven(day_num)
        .then(response => {
          this.earn7.days = response.data.days
          this.earn7.totals = response.data.totals
          this.$nextTick(function() {
            this.drawLine()
          })
        })
      order.getCategoryRatio(day_num)
        .then(response => {
          this.categoryRatio.names = response.data.names
          this.categoryRatio.payments = response.data.payments
          this.categoryRatio.total = response.data.total
          this.categoryRatio.seriesData = []
          for (let i = 0; i < this.categoryRatio.names.length; i++) {
            this.categoryRatio.seriesData.push({ 'value': this.categoryRatio.payments[i], 'name': this.categoryRatio.names[i] })
          }
          this.$nextTick(function() {
            this.drawPie()
          })
        })
    },
    drawLine() {
      this.line_chart = echarts.init(document.getElementById('line_chart'))
      this.line_chart.setOption({
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['近' + this.day_num + '日收益']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: this.earn7.days
        },
        yAxis: {
          type: 'value'
        },

        series: [{
          name: '该日收益',
          type: 'line',
          stack: '总量',
          data: this.earn7.totals
        }]
      })
    },
    drawPie() {
      // 基于准备好的dom，初始化echarts实例
      this.pie_chart = echarts.init(document.getElementById('pie_chart'))
      // 绘制图表
      this.pie_chart.setOption({
        title: {
          text: '近' + this.day_num + '天用户购买的商品分类占比', // 主标题
          subtext: '近' + this.day_num + '天总收入 ￥' + this.categoryRatio.total,
          x: 'center'// x轴方向对齐方式
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          orient: 'vertical',
          bottom: 'bottom',
          data: this.categoryRatio.names
        },
        series: [
          {
            name: '访问来源',
            type: 'pie',
            radius: '55%',
            center: ['50%', '60%'],
            data: this.categoryRatio.seriesData,
            itemStyle: {
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ],
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        }
      })
    }
  }
}
</script>
<style scoped>
* {
  margin: 0;
  padding: 0;
  list-style: none;
}
</style>
